$(function () {
    $('body').on('change','#areaType_0',function () {
        var value = this.value;
        if(value == 0){
            selectArea(1);
            return;
        }
        queryAddress(value, 1, 0);
    }).on('change','#areaType_1',function () {
        var value = this.value;
        if(value == 0){
            selectArea(2);
            return;
        }
        queryAddress(value, 2, 0);
    });
});
/**
 * 查询省/市/县
 * @param parentId 父行政区划id
 * @param areaType 0.省级 1.市级 2.县级
 * @param select 默认值
 * @param callback 下级行政区划回调
 */
function queryAddress(parentId, areaType, select, callback) {
    $.ajax({
        url: baseloaction + "/web/uc/ajax/queryAreaListByParentId",
        type: "post",
        dataType: "json",
        data: {
            "parentId": parentId,
            'areaType': areaType
        },
        success: function (result) {
            if (result.success == true) {
                var userAddress = result.entity;
                //switch fall-through
                selectArea(areaType);
                if (userAddress && userAddress.length > 0) {
                    for (var i = 0; i < userAddress.length; i++) {
                        $('#areaType_' + areaType).append('<option value="' + userAddress[i].id + '">' + userAddress[i].areaName + '</option>');
                    }
                }
                if (select) $('#areaType_' + areaType).val(select);
                if ($.isFunction(callback)) callback(select);
            }
        }
    });
}

/**
 * 选择地区时，选择默认值，清空所有下级市县下拉框
 * @param areaType 地区类型 0-省、1-市、2-县
 */
function selectArea(areaType) {
    switch (areaType) {
        case 0 :
            $('#areaType_' + 0).html('<option selected value="0">--请选择省--</option>');
        case 1 :
            $('#areaType_' + 1).html('<option selected value="0">--请选择市--</option>');
        case 2 :
            $('#areaType_' + 2).html('<option selected value="0">--请选择县--</option>');
            break;
    }
}

/**
 * 保存用户地址信息
 * @param id
 * @param callback
 */
function initSaveAddress(id,callback) {
    var title ='增加收货地址';
    var url = baseloaction +'/web/uc/initUpdate?addressId='+id;
    if(id>0){
        title ='修改收货地址'
        url = baseloaction +'/web/uc/initUpdate?addressId='+id;
    }
    layer.open({
        type: 2,
        title: title+'<span class="ml10 fs12 c-999"><small class="fs12 c-danger">*</small>为必填项</span>',
        area:['960px','480px'],
        shadeClose: true,
        maxmin:true,
        scrollbar: false,
        skin: 'layui-layer-demo',
        content: [url,'no'],
        btn: [' 确定 ', ' 取消 '],
        yes: function (index, layero) {
            var frame = parent.layer.getChildFrame('body', index);
            var params = frame.find('#addUserAddress').serializeJSON();
            params = JSON.stringify(params);
            $.ajax({
                url: baseloaction + "/web/uc/ajax/saveUserAddress",
                data: params,
                dataType: "json",
                type: "post",
                contentType:'application/json;charset=utf-8',
                success: function (result) {
                    icModale(0, "", result.message, null);
                    if (result.success == true) {
                        //重新加载用户收货地址
                        if($.isFunction(callback)){
                            callback();
                        }
                        layer.close(index);
                    }
                }
            });
        },
        btn2: function (index, layero) {
            layer.close(index)
        }
    });
}
